#ifndef __VARIABLEMIDDLE_H
#define __VARIABLEMIDDLE_H

#include <stdio.h>
#include <stdint.h>
#include <string.h>

#include "SWM341.h"
#include "config.h"
#include "CirleBuffer.h"

#define Display_version  "BMS_Dis_20231012"  //仪表版本号

#define HEAD_ID       0X02
#define BLE_NAME_LEN  16
#define SET_BLE_NAME  "BMS" //蓝牙默认名称：
#define BLUE_ENABLE    1  //开启蓝牙功能

#define OFFLINE_OUT_TIME  6000  //离线清零数据时间定义，单位ms

//*********************************************************************
#define DISPLAY_MODE 2  //显示模式选择 
//********
#if DISPLAY_MODE==0 // 蓝色背景图配置
#define LV_COLOR_bg  LV_COLOR_BLUE  //  背景颜色
#define LABEL_COLOR  LV_COLOR_WHITE //  字体颜色
#define LINE_COLOR   LV_COLOR_BLACK //  线段颜色

#elif DISPLAY_MODE==1 // 白色背景图配置
#define LV_COLOR_bg  LV_COLOR_WHITE // 背景颜色
#define LABEL_COLOR  LV_COLOR_BLUE // 字体颜色
#define LINE_COLOR   LV_COLOR_BLUE // 线段颜色

#elif DISPLAY_MODE==2  //
#define LV_COLOR_bg  LV_COLOR_WHITE //背景颜色
#define LABEL_COLOR  LV_COLOR_BLUE // 字体颜色
#define LINE_COLOR   LV_COLOR_BLACK // 线段颜色

#elif DISPLAY_MODE==3  //
#define LV_COLOR_bg  LV_COLOR_WHITE //背景颜色
#define LABEL_COLOR  LV_COLOR_BLACK // 字体颜色
#define LINE_COLOR   LV_COLOR_BLUE // 线段颜色

#endif

#define SET_GET_PER  300 //数据通讯更新时间间隔，单位ms

#define RX_LEN    128
#define VER_LEN   60

#define BLZ_IN  ( ( ( (GPIOD)->IDR ) >> (PIN1) ) & 0x01 )
//***********************************
#define GET_SOFT_VER  0X4E20
#define MAIN_ORDER_A  0X4E28
#define MAIN_ORDER_B  0X4E31
#define GET_ERROR       0X4E48

#define GET_DATE        0X4F67
#define GET_INFO_A      0X4EE8
#define GET_INFO_B      0X4EF0

#define GET_CELL_L1   0X5208
#define GET_CELL_L2   0X5210
#define GET_CELL_L3   0X5218
#define GET_CELL_L4   0X5220

extern char 	MAC_DATA[16];
extern const  char GET_MAC[];
extern const  char BLE_RESET[];
extern const  char BLE_ADD[];
extern const  char BLE_PID[];
extern const  char *BLE_SET_NAME;
extern char 	BLE_DEF_NAME[];
extern char 	BLE_RE_NAME[];
extern char 	BLE_GET_NAME[];
extern char 	BLE_READ_NAME[BLE_NAME_LEN];


extern uint8_t RX_Buffer[2][RX_LEN], send_index, current_page_index, send_tx_time, send_tx_len ;
extern CircleBuffer_t CirBuf;
//*****************************************************
typedef struct
{
    uint8_t Lenth;
    uint8_t Data[RX_LEN]; /*!< Contains the data to be received. It ranges from 0 to
                          0xFF. */
} UART_TYPE;

extern    UART_TYPE  receve_order0, uart_data0, uart_data_blue, receve_order_blue, Uart_send_data, uart_temp;
typedef struct
{
    uint8_t Lenth;
    uint8_t err[5];
    uint16_t odo[5];
} Faults_data;
extern Faults_data faults;
//***************************************************************************
typedef struct
{
    volatile unsigned char       open_flag: 1;
    volatile unsigned char       uart0_receive_flag: 1;
    volatile unsigned char       uart_blue_receive_flag: 1;
    volatile unsigned char       buzzer: 1;
    volatile unsigned char       touch_buzzer: 1;
    volatile unsigned char       send_tx_dma: 1;
    volatile unsigned char       send_run_flag: 1;
    volatile unsigned char       get_data_flag: 1;
    volatile unsigned char       mac_flag: 1;
    volatile unsigned char       get_name_flag: 1;
    volatile unsigned char       ble_d: 1;
    volatile unsigned char       send_blue_tx_dma: 1;
    volatile unsigned char       blue_connect: 1;
    volatile unsigned char       scan_start_flag: 1;
    volatile unsigned char       scaning_flag: 1;

    volatile unsigned char       connect_start: 1;
    volatile unsigned char       def_mac_flag: 1;
    volatile unsigned char       ble_off_line: 1;
    volatile unsigned char       page_index_change: 1;
	  volatile unsigned char       scan_page_show: 1;
	 	volatile unsigned char       off_line: 1;
		volatile unsigned char       clear_page_data: 1;
		

} s_type;
extern s_type s;

typedef struct
{
    uint8_t Lenth;
    uint8_t Data[32];
} Verson_data;

extern Verson_data DP_S_VER, BIND_INF;

typedef struct
{
    uint16_t    VOLT;
    int16_t    Curr;
    uint16_t    SOC ;
    uint16_t    FltCode1;
    uint16_t    FltCode2;
    uint32_t    MaxVolt;
    uint32_t    MinVolt;
    int16_t    MaxTemp;
    int16_t    MinTemp;

} bms_main_type;
extern bms_main_type bms_main_data;

typedef struct
{
    uint16_t    vol[30];
} bms_cell_type;
extern bms_cell_type bes_cell_data;

typedef struct
{
    uint8_t     Uniformity;
    uint16_t    PowerVoltage;
    uint16_t    SOH;
    uint16_t    DeepCycleNum ;
    uint32_t    ODO;
    uint32_t    TRIP;
    uint32_t    ODO_Consumption;
    uint32_t    TRIP_Consumption;
    uint32_t    ODO_AVG_Consumption;
    uint32_t    TRIP_AVG_Consumption;
    uint8_t     Gatheringline;
    uint16_t    InSideCAN;
    uint16_t    Heating;
    uint8_t     Cooling;

} bms_info_type;
extern bms_info_type bms_info_data;

typedef struct
{
    char  bms_sw[VER_LEN];
    char  bms_hw[VER_LEN];
    char  bms_code[VER_LEN];
    char  bms_date[VER_LEN];

} bms_version_type;

typedef struct
{
    uint8_t  	Lenth;
    uint8_t  	order_out_time[20];
    bool 		 	status[20];
    uint16_t 	order[20];
    uint8_t 	Get_data_len[20];

} Send_order_type;

extern Send_order_type DEF_Send_order_data, Send_order_data[];


extern bms_version_type bms_version_data;

typedef struct
{
    char  	name[20];
    char  	 mac[12];
    uint8_t 	mac_hex[6];
} def_ble_device_type;
extern def_ble_device_type def_device, eeprom_def_device;

typedef struct
{
    uint8_t  id;
    char  name[20];
    char  mac[12];
    uint8_t mac_hex[6];
    char  list[60];
} ble_device_type;
extern ble_device_type Device_list[];

typedef struct
{
    char  sw[VER_LEN];
    char  hw[VER_LEN];
    char  name[VER_LEN];
    char  mac[VER_LEN];

} dsp_version_type;
extern dsp_version_type dsp_version_data;
extern char dsp_ver_info[2][VER_LEN] ;

extern uint16_t get_name_time, mac_time,off_line_time;

extern uint16_t stateflag, stateflag_blue, re_send_order, buzzer_time, buzzer_set_time, send_run_time, get_data_time;
extern void Uart0_DMA_Config(uint16_t rx_len, uint8_t *rx_Buffer);
extern void Uart2_DMA_Config(uint16_t rx_len, uint8_t *rx_Buffer);
extern uint8_t get_device_num, show_device_num, wait_out_time;

#endif
//*******************************************//

